package com.beagle.njddapp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.IBinder;
import android.text.InputType;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.beagle.njddapp.R;
import com.beagle.njddapp.base.BaseApplication;
import com.beagle.njddapp.base.BaseMvpActivity;
import com.beagle.njddapp.core.UserService;
import com.beagle.njddapp.ui.presenter.LoginPresenter;
import com.beagle.njddapp.ui.view.LoginView;
import com.beagle.njddapp.utils.CountDownUtil;
import com.beagle.njddapp.utils.PrefsManager;
import com.beagle.njddapp.utils.StatusBarUtil;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

import static com.beagle.njddapp.core.UserService.TOKEN;

/**
 * @author hxx
 * @date 2021-03-02 22:31
 * 登录
 */
public class ChangePasswordActivity extends BaseMvpActivity<LoginView, LoginPresenter> implements LoginView {

    @BindView(R.id.ed_phone)
    TextView edPhone;
    @BindView(R.id.ed_pwd)
    EditText edMsg;
    @BindView(R.id.commom_title_tv)
    TextView tvTitle;
    private String phone,code;

    @Override
    public int getLayoutId() {
        return R.layout.activity_change_password;
    }

    @Override
    public void init() {
        StatusBarUtil.immersive(this, Color.WHITE);
        StatusBarUtil.darkMode(this);
        phone=getIntent().getStringExtra("phone");
        code=getIntent().getStringExtra("code");
        tvTitle.setText("修改密码");
    }

    @Override
    public LoginView getMvpView() {
        return this;
    }

    @Override
    public LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void onError(String message) {
        ToastUtils.showShort(message);
    }

    @OnClick({R.id.tv_login,R.id.common_back_iv})
    public void onCLick(View view){
        switch (view.getId()){
            case R.id.common_back_iv:
                onBackPressed();
                break;
            case R.id.tv_login:
                if (TextUtils.isEmpty(edMsg.getText().toString())||edMsg.getText().toString().length()<6){
                    ToastUtils.showShort("请输入最少6位的密码");
                    return;
                }
                if (TextUtils.isEmpty(edPhone.getText().toString())||edPhone.getText().toString().length()<6){
                    ToastUtils.showShort("请输入最少6位的密码");
                    return;
                }
                if (!edPhone.getText().toString().equals(edMsg.getText().toString())){
                    ToastUtils.showShort("两次输入的密码不一样");
                    return;
                }
                presenter.resetPwd(phone,code,edMsg.getText().toString());
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideKeyboard(v, ev)) {
                hideKeyboard(v.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }


    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击EditText的事件，忽略它。
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
    /**
     * 获取InputMethodManager，隐藏软键盘
     * @param token
     */
    private void hideKeyboard(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        edPhone.setText(UserService.getInstance().getPhone());

    }

    @Override
    public void toMian() {
        ToastUtils.showShort("密码重置成功，请使用新密码登录");
        edPhone.postDelayed(new Runnable() {
            @Override
            public void run() {
                UserService.getInstance().setUserInfoBean(null);
                UserService.getInstance().setRole(-1);
                UserService.getInstance().setToken("");
                UserService.getInstance().setPhone("");
                PrefsManager.getInstance(BaseApplication.getInstance()).putString(TOKEN,"");
                PrefsManager.getInstance(getApplicationContext()).clear();
                ActivityUtils.finishAllActivities();
                UserService.getInstance().setPhone(phone);
                startActivity(new Intent(ChangePasswordActivity.this,PasswordLoginActivity.class));
                finish();
            }
        },1000);
    }
    @Override
    public void toChooseRole() {

    }

    @Override
    public void toInfo() {

    }
}
